package cn.lyq.card.ui.advice

import androidx.lifecycle.Observer
import cn.lyq.card.R
import cn.lyq.card.base.BaseActivity
import cn.lyq.card.data.src.Status
import cn.lyq.card.ui.result.ResultActivity
import cn.lyq.card.utils.setClick
import com.blankj.utilcode.util.ToastUtils
import kotlinx.android.synthetic.main.act_advice.*
import kotlinx.android.synthetic.main.act_bank_card_add.tvConfirm


class AdviceActivity : BaseActivity() {

    private val mModel by lazy {
        getVM(AdviceVM::class.java)
    }

    override fun getLayoutId(): Int {
        return R.layout.act_advice
    }

    override fun initView() {
        super.initView()
        setMyTitle(R.string.str_advice)
    }

    override fun initListener() {
        super.initListener()
        tvConfirm.setClick {
            val advice = etAdvice.text.toString().trim()
            if (advice.isEmpty()) {
                ToastUtils.showShort(etAdvice.hint)
                return@setClick
            }
            commitAdvice(advice)
        }
    }

    private fun commitAdvice(advice: String) {
        mModel.addAdvice(advice).observe(this, Observer {
            when (it?.status) {
                Status.SUCCESS -> {
                    dismissLoading()
                    ResultActivity.start(this, 5)
                    finish()
                }
                Status.LOADING -> showLoading()
                Status.ERROR -> {
                    ToastUtils.showShort(it.message.getMsg())
                    dismissLoading()
                }
            }
        })
    }
}